<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
	</head>

	<body style="padding: 0px;">
		<div id="toolbar" class="receipts_toolbar_style"></div>
		<div>
			<form id="ff" method="post">
				<div style="margin-left: 15%;padding: 30px 0px;">
					<table cellpadding="5" style="width: 85%;">
						<tr>
							<td width="10%" style="display: none">合同ID</td>
							<td width="23%" style="display: none">
								<input style="width:100%;display: none;" class="easyui-textbox" type="text" id="input_ID" name="ID" data-options="required:true,prompt:'由系统自动生成，不需填写',disabled:true"></input>
							</td>
							<td width="10%">合同编号</td>
							<td width="23%">
								<input style="width:100%" class="easyui-textbox" id="input_ORDER_NO" name="ORDER_NO" data-options="required:true,prompt:'由系统自动生成，不需填写',disabled:true"></input>
							</td>
							<td width="10%">合同年限</td>
							<td width="23%">
								<input style="width:100%" class="easyui-numberbox" type="text" suffix="年" id="input_CONTRACT_LIFE" name="CONTRACT_LIFE" data-options="required:true,prompt:'由系统自动生成，不需填写',disabled:true"></input>
							</td>
							</td>
						</tr>
						<tr>
							<!--<td width="10%">合同期限</td>
							<td width="23%">
								<input style="width:100%" class="easyui-textbox" type="contract_term" id="input_DEADLINE" name="DEADLINE" data-options="required:true"></input>
							</td>-->
							<td width="10%">客户名称</td>
							<td width="23%">
								<input style="width:100%" class="easyui-textbox" type="text" id="input_TENANT_NAME" name="TENANT_NAME" data-options="required:true"></input>
							</td>
							<td width="10%">开始时间</td>
							<td width="23%">
								<input style="width:100%" class="easyui-datebox" required data-options="validType:'md[\'10/11/2015\']'" id="input_BEGIN_TIME" name="BEGIN_TIME"></input>
							</td>
							<td width="10%">结束时间</td>
							<td width="23%">
								<input style="width:100%" class="easyui-datebox" required data-options="validType:'md[\'10/11/2015\']'" id="input_END_TIME" name="END_TIME"></input>
							</td>
						</tr>
						<tr>
							<td width="10%">租金总额</td>
							<td width="23%">
								<input style="width:100%" class="easyui-numberbox" id="input_RENT_SUM" name="RENT_SUM" data-options="required:true,disabled:true" prefix="￥"></input>
							</td>
							<td width="10%">保证金总额</td>
							<td width="23%">
								<input style="width:100%" class="easyui-numberbox" type="text" id="input_MARGIN_SUM" name="MARGIN_SUM" data-options="required:true,disabled:true" prefix="￥"></input>
							</td>
							<td width="10%">其他费用合计</td>
							<td width="23%">
								<input style="width:100%" class="easyui-numberbox" type="text" id="input_OTHERS_SUM" name="OTHERS_SUM" data-options="required:true,disabled:true" prefix="￥"></input>
							</td>
						</tr>
					</table>
				</div>
				<table id="tt">

				</table>
				<div style="padding: 10px 0px;">
					<table>
						<tr>
							<th>人民币金额合计</th>
							<td>
								<input type="text" style="border: none;" name="number" />
							</td>
							<th>人民币金额合计(大写)</th>
							<td width="auto">
								<input type="text" style="border: none;" id="dx" />
							</td>
						</tr>
					</table>
				</div>
				<div id="new_footer" style="margin-left: 23%;margin-top: 50px;"></div>
			</form>
		</div>
		<div id="dlg" class="hiddenDialog">
			<iframe id="ifrmEdit" style="width: 100%;height: 100%;" frameborder="0" scrolling="no"></iframe>
		</div>
		<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../../JS/finance.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
		<script type="text/javascript" src="../../../JS/common.js"></script>
		<script type="text/javascript" src="../../../JS/SHA-1.js"></script>
		<script type="text/javascript" src="../../../JS/MarketShareFunction/contractShareFunction.js"></script>
	</body>
	<script type="text/javascript">
		var currid = AddFlag; //当前页面的ID
		var id = getID(); //获取上个页面传回的ID
		console.log(id);
		var currPage = 0; //当前的页数
		var MaxPage = 0;
		var MinPage = 1;
		var isVerify;
		var chargCount = 0; //收费的次数
		var currRow;
		var a = new Object();
		var _a = JSON.stringify(a);
		localStorage.setItem("shortTermContractData", _a); //初始化合同数据
		var typeInfo = new Array(); //将typeInfo初始化为数组
		var _detailToolbarData;
		var auth = "admin;" + hex_sha1("admin1");
		_detailToolbarData = AddDetailToolBar(15);

		var data = [{
			"icon": "icon-save",
			"func": "save()",
			"functionname": "保存"
		}];

		$('#input_TENANT_NAME').combobox({
			valueField: 'NAME',
			textField: 'NAME',
			panelHeight: 'auto',
			onChange: function(newValue, oldValue) {
				if(newValue != "" && newValue != undefined) {
					var url = RemoteLinkAddress + "/market/tenant/findbypinyin?PINYINFIELDNAME=" + newValue.trim();
					$(this).combobox("reload", url);
				}
			},
			onSelect: function(record) {
				console.log(record);
				TENANT_ID = record.ID;
			}
		});

		$("#input_END_TIME").datebox({
			valueField: 'BEGIN_TIME',
			textField: 'BEGIN_TIME',
			onSelect: function(date) {
				var begintime = $("input[name='BEGIN_TIME']").val();
				var endtime = $("input[name='END_TIME']").val();
				var peroid = CalTimeLimit(begintime, endtime, TIME_YEAR);
				$("#input_CONTRACT_LIFE").numberbox('setValue', peroid);
			}
		})

		GetTable();
		$(document).ready(function(){
			if(id != AddFlag && id != undefined) {
				currid = id;
				loadRemote();
			}
		});
		if(id == undefined) {
			currid = AddFlag;
			$('#tt').edatagrid("iniRow");
		}
		
		addFooter($("#new_footer"));
		//增加单据的toolba
		///tooldata为新增的toolbar数据
		//		var receipts_types = RECEIPT_CHARGE_SYMBOL;
		content = AddReceiptsToolBar(0, data);
		$("#toolbar").append(content); /*在ID=toolbar的标记后面添加content中的内容*/

		function newdata() {
			currid = AddFlag;
			currPage = 0;
			$('#ff').form('reset');
			GetTable();
		}

		function getRemoteData(_url) //统一获取远程的数据
		{
			$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
				type: "get",
				url: _url,
				dataType: "json", //服务器返回的数据类型
				success: function(result) {
					console.log(result);
					if(result != null) {
						getFormData(result);
						currid = result.ID;
						currPage = result.ID;
						isVerify = result.ISVERIFY;
						var _data = result.rowsBoost;
						$('#tt').datagrid('loadData', _data);
						console.log(result);
					}

				}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}
		//////////
		function loadRemote() { //载入服务器上的数据，用于修改以及查看更多的时候调用，根据id是否等于-1来判断
			var _url = RemoteLinkAddress + '/market/contract/findbyid.do?ID=' + currid;
			getRemoteData(_url);
		}

		$('#input_sum').textbox({
			onChange: function(value) {
				var _trim = $.trim(value);
				var s = changeMoneyToChinese(_trim);
				$('#span_sum').html(s);
			}
		});

		function GetTable() {
			$('#tt').edatagrid({
				height: 280,
				//				width: '100%',
				title: '短期合同明细',
				toolbar: _detailToolbarData,
				singleSelect: 'true', //是否单选：是
				rownumbers: 'true',
				fitColumns: 'true', //自动调整列：是
				showFooter: 'true',
				autoSave: 'true',
				columns: [
					[{
						field: 'ID',
						title: 'ID',
						width: '0%',
						align: 'center',
					}, {
						field: 'ORDER_ID',
						title: 'ORDER_ID',
						width: '0%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "ORDER_ID",
								textField: "ORDER_ID",
								editable: false,
							}
						}
					}, {
						field: 'SHORT_ID',
						title: 'SHORT_ID',
						width: '0%',
						align: 'center',
						editor: {
							type: 'textbox',
							options: {
								valueField: "SHORT_ID",
								textField: "SHORT_ID",
								editable: false,
							}
						}
					}, {
						field: 'SHORT_NAME',
						title: '短期合同名称',
						width: '8%',
						align: 'center',
						editor: {
							type: 'combobox',
							options: {
								valueField: "NAME",
								textField: "NAME",
								url: RemoteLinkAddress + "/market/shorttype/listall.do",
								required: true,
							}
						}
					}, {
						field: 'RENT',
						title: '租金',
						width: '12%',
						align: 'center',
						editor: {
							type: 'numberbox',
							options: {
								required: true,
							}
						}
					}, {
						field: 'MARGIN',
						title: '保证金',
						width: '12%',
						align: 'center',
						editor: {
							type: 'numberbox',
							options: {
								required: true,
							}
						}
					}, {
						field: 'OTHERS_CHARGE',
						title: '其他费用',
						width: '12%',
						align: 'center',
						editor: {
							type: 'numberbox',
							options: {
								required: true,
							}
						}
					}, {
						field: 'BEGIN_TIME',
						title: '开始时间',
						width: '12%',
						align: 'center',
						editor: {
							type: 'datebox',
							options: {
								required: true,
							}
						}
					}, {
						field: 'END_TIME',
						title: '结束时间',
						width: '12%',
						align: 'center',
						editor: {
							type: 'datebox',
							options: {
								required: true,
							}
						}
					}, {
						field: 'RENT_END',
						title: '实际租用结束时间',
						width: '13%',
						align: 'center',
						editor: {
							type: 'datebox',
							options: {
								required: true,
								editable: false,
								disabled: true
							}
						}
					}, {
						field: 'SUM',
						title: '小计',
						width: '14%',
						align: 'center',
						editor: {
							type: 'numberbox',
							options: {
								required: false,
								editable: false,
							}
						}
					}]
				],
				onDblClickRow: function(index, row) {
					currRow = row;
				},
				onAdd: function(index, row) {
					currRow = row;
				},
				onEndEdit: function(index, row, changes) {
					var result = parseFloat(row.RENT) + parseFloat(row.MARGIN) + parseFloat(row.OTHERS_CHARGE);
					$('#tt').edatagrid('updateRow', {
						index: index,
						row: {
							SUM: result
						}
					});
					$('#tt').edatagrid('refreshRow', index);
					autoCalculate(row, index);
				}
			});
			$("#tt").datagrid('hideColumn', 'ID');
			$("#tt").datagrid('hideColumn', 'ORDER_ID');
			$("#tt").datagrid('hideColumn', 'SHORT_ID');
		}

		var auth = "admin;" + hex_sha1("admin1");

		function save() {
			var a = checkDataIntegrity();
			if(!a) {
				$.messager.alert("警告", "短期合同信息不完整！");
				return;
			}
			if(chargCount === 0) {
				$.messager.alert("警告", "请完成收费操作！");
				return;
			}
			getData();
		}

		function getData() {
			var data = JSON.parse(localStorage.getItem("shortTermContractData"));
			data.TENANT_NAME = $("input[name='TENANT_NAME']").val();
			data.BEGIN_TIME = $("input[name='BEGIN_TIME']").val();
			data.END_TIME = $("input[name='END_TIME']").val();
			data.BUSINESS_AREA = $("input[name='BUSINESS_AREA']").val();
			data.RENT_SUM = $("input[name='RENT_SUM']").val();
			data.MARGIN_SUM = $("input[name='MARGIN_SUM']").val();
			data.OTHERS_SUM = $("input[name='OTHERS_SUM']").val();
			data.CONTRACT_LIFE = $("input[name='CONTRACT_LIFE']").val();
			data.AUTHORIZATION = auth;
			data.USERNAME = "admin";
			data.ORDER_TYPE = "0"; //0代表短期
			data.CONTRACT_TYPE = "新建";
			data.rowsShort = new Array();
			var dataGridRows = $("#tt").datagrid("getRows");
			console.log(dataGridRows);
			for(var i = 0; i < dataGridRows.length; i++) {
				if(typeof(dataGridRows[i].RENT) != "undefined") {
					var _index = i;
					data.rowsShort[_index] = new Object();
					data.rowsShort[_index].SHORT_NAME = dataGridRows[i].SHORT_NAME;
					data.rowsShort[_index].RENT = dataGridRows[i].RENT;
					data.rowsShort[_index].BEGIN_TIME = dataGridRows[i].BEGIN_TIME;
					data.rowsShort[_index].END_TIME = dataGridRows[i].END_TIME;
					data.rowsShort[_index].MARGIN = dataGridRows[i].MARGIN;
					data.rowsShort[_index].OTHERS_CHARGE = dataGridRows[i].OTHERS_CHARGE;
					data.rowsShort[_index].SUM = dataGridRows[i].SUM;
				}
			}
			console.log(data);
			var totalCharge = getTotalCharge(data);
			data.SUM = totalCharge;
			localStorage.setItem("shortTermContractData", JSON.stringify(data));
			submitData(data);
		}

		function submitData(_data) {
			var _senddata = JSON.stringify(_data);
			$.ajax({
				dataType: 'JSON',
				url: RemoteLinkAddress + "/market/contract/save.do",
				data: 'a=' + _senddata,
				type: "POST",
				success: function(result) {
					if(result.msg.search("success") > -1) {
						$.messager.alert("提示信息", "保存成功");
					} else {
						$.messager.alert("错误信息", "保存失败");
					}
				},
				error: function(result) {
					$.messager.alert("错误信息", "保存失败");
				}
			});
		}

		var dialogWidth = 400;
		var dialogHeight = 300;
		var AddDialog;

		function AddDict() {
			var pLocation = "UI/COMMON/DictData.html?id=-1";
			$("#ifrmEdit").attr("src", pLocation);
			constructDialog($("#dlg"), "添加基础信息", dialogWidth, dialogHeight);
			AddDialog.dialog('open');
		}

		function charge() {
			var pLocation = "UI/Market/BIContractCreate/BIContractAddShortCharge.html";
			$("#ifrmEdit").attr("src", pLocation);
			constructDialog($("#dlg"), "新建短期合同收费管理", 600, 310);
			AddDialog.dialog('open');
			chargCount++;
		}

		function checkDataIntegrity() {
			var customername = $("input[name='CUSTOMER_NAME']").val();
			var begintime = $("input[name='BEGIN_TIME']").val();
			var endtime = $("input[name='END_TIME']").val();
			var businessarea = $("input[name='BUSINESS_AREA']").val();
			var rentsum = $("input[name='RENT_SUM']").val();
			var marginsum = $("input[name='MARGIN_SUM']").val();
			var otherssum = $("input[name='OTHERS_SUM']").val();
			var rowsdata = $("#tt").datagrid('getRows');
			console.log(rowsdata);
			if(customername != "" && begintime != "" && endtime != "" && businessarea != "" && rentsum != "" && marginsum != "" && otherssum != "") {
				if(rowsdata.length != 0) {
					for(var i = 0; i < rowsdata.length; i++) {
						var shorttype = rowsdata[i].SHORT_NAME;
						var region = rowsdata[i].REGION;
						var regiontype = rowsdata[i].REGION_TYPE;
						var housenumber = rowsdata[i].HOUSE_NUMBER;
						var businessarea = rowsdata[i].BUSINESS_AREA;
						var rent = rowsdata[i].RENT;
						var margin = rowsdata[i].MARGIN;
						var othercharge = rowsdata[i].OTHERS_CHARGE;
						var begintime = rowsdata[i].BEGIN_TIME;
						var endtime = rowsdata[i].END_TIME;
						var sum = rowsdata[i].SUM;
//						if(sum != "") {
							if(shorttype == "" || region == "" || regiontype == "" || housenumber == "" || businessarea == "" || rent == "" || margin == "" || othercharge == "" || begintime == "" || endtime == "") {
								return false;
							}
//						}
					}
				} else {
					return false;
				}
			} else {
				return false;
			}
			return true;
		}

		function getTotalCharge(totalData) {
			//得到总价
			var sum = parseFloat(totalData.RENT_SUM) + parseFloat(totalData.MARGIN_SUM) + parseFloat(totalData.OTHERS_SUM);
			return sum;
		}
	</script>

</html>